package iscte.iul.dcti.pa.al2011_12.viewer;

import java.text.Format.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Observable;
import java.util.Observer;

import iscte.iul.dcti.pa.al2011_12.model.Cell;
import iscte.iul.dcti.pa.al2011_12.model.Row;
import iscte.iul.dcti.pa.al2011_12.model.Sheet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.omg.CORBA.PRIVATE_MEMBER;

import java.lang.reflect.*;

import javax.swing.text.html.HTMLDocument.Iterator;

/**
 * Este codigo foi simplesmente copiado de um exemplo
 * Pretende.se que seja modificado no exercicio
 *
 */
public class DynamicTable extends Composite {	//extends Observable
	
	private Table table;
	private Observer observer;
	
	public DynamicTable(Composite parent) {
		super(parent, SWT.NONE);
		
		setLayout(new GridLayout());
		table = new Table (this, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible (true);
		table.setHeaderVisible (true);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		table.setLayoutData(data);
		load(null);
	}
	

	public void load(Sheet sheet) {
		if (sheet != null) {
			
			char currentColumn = 'A';
			for (int i = 0; i < sheet.getNumColumns(); i++) {
				new TableColumn(table, SWT.NONE).setText(Character.toString((currentColumn++)));
			}
		
			int i;
			TableItem tableItem;
			Row row;
			java.util.Iterator<?> itr = sheet.getRows().iterator();
			while (itr.hasNext()) {
				//Object object = (Object) itr.next();
				i = 0;
				row = (Row) itr.next();
				tableItem = new TableItem(table, SWT.NONE);
				//object.
				for (Cell cell : row.getCells()) {
					tableItem.setText(i++, cell.getValue());
					//tableItem.
				}
			}
			
			for (int j = 0; j < sheet.getNumColumns(); j++) {
				table.getColumn(j).pack();
			}
		}
	}
	
	public Table getTable() {
		return table;
	}
	
	
}
